package com.example.smartcity_poverty.ui.AllService.Attorneys.activity.AttMe.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.model.Attreneys.AttMeListUserModel
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_att_me_all.*
import kotlinx.coroutines.Dispatchers

/**
 * 咨询详情页
 */
class AttMeAllActivity : AppCompatActivity() {
    private var id: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_att_me_all)
        title = "咨询详情页"
        id = intent.getStringExtra("id")?.toInt()

        // 启动协程发起网络请求
        runCoroutine()
    }

    /**
     * 协程管理
     */
    private fun runCoroutine() = coroutine(Dispatchers.IO){
        // 网络请求获取我的咨询详情
        getHttpMeAll()
    }

    /**
     * 网络获取我的咨询详情
     */
    // 存储数据
    private suspend fun getHttpMeAll() {
        // 判断传输过来的id是否为空！
        if (id.toString().isNotEmpty()){
            // 网络请求
            val result = api.getAttMeListXQ(getToken(), id!!)
            runOnUiThread {
                // 填充数据
                result.data?.let {
                    attAllUserName.text = it.lawyerName
                    attAllUserTime.text = "从业年限: ${it.createTime}"
                    attAllUserSize.text = "咨询人数: 12人"
                    attAllUserThis.text = "法律专长: ${it.legalExpertiseName}"
                    attAllUserNumber.text = "好评率： 1%"
                    if (it.state.toString() == "0") attAllIsOk.text = "处理中..."
                    else {
                        attAllBtnOk.visibility = View.VISIBLE
                        attAllIsOk.text = "已处理。"

                        val dd = it.id
                        val legalExpertiseId = it.legalExpertiseId
                        attAllBtnOk.setOnClickListener {
                            startActivity(Intent(this, AttSetAllActivity::class.java).apply {
                                putExtra("id", dd.toString())
                                putExtra("legalExpertiseId", legalExpertiseId.toString())
                            })
                        }
                    }
                    attAllClass.text = it.legalExpertiseName
                    attAllContent.text = it.content
                    attAllPhone.text = it.phone
                    attAllImageView.setImageResource(R.mipmap.b1)
                }

            }
        }else runOnUiThread { "加载出错了！".show() }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}